﻿package net.wangit.adm.role;

import net.wangit.context.ActionContext;
import net.wangit.jdbc.ColumnRow;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.Table;
import net.wangit.support.action.DefaultAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.group.UserGroupCache;
import net.wangit.user.org.OrgCache;
import net.wangit.user.role.RoleCache;
import net.wangit.util.Checker;
import net.wangit.util.StringUtil;

public class RoleAuthAction extends DefaultAction {

	private static final String SQL_CLEAR = "delete from AROLEPRIVILEGE where roleid=? and domain=?";

	public RoleAuthAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String roleId = ac.getRequestParameter("role.privileges.roleid");
		String domain = ac.getRequestParameter("role.privileges.domain");
		if (Checker.isEmpty(roleId) || Checker.isEmpty(domain))
			return;
		db.execute("delete from AROLEPRIVILEGE where roleid=? and domain=?",
				new String[] { roleId, domain }, new int[] { 2, 2 });
		String checks[] = ac.getRequestParameters("_treecheck");
		if (!Checker.isEmpty(checks)) {
			Table table = new Table("adm", "AROLEPRIVILEGE");
			for (int i = 0; i < checks.length; i++)
				if (!"-".equals(checks[i])) {
					String privileges[] = StringUtil.split(checks[i], ",");
					ColumnRow row = table.addInsertRow();
					row.set("roleid", roleId);
					row.set("domain", domain);
					row.set("type", privileges[1]);
					row.set("rid", privileges[2]);
					row.set("pid", privileges[3]);
				}

			db.save(table);
		}
		RoleCache.clear(roleId);
		OrgCache.clearAll();
		UserGroupCache.clearAll();
		AdminCache.clearAll();
	}
}
